import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store";

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/skillHome',
    name: 'skillHome',
    component: () => import(/* webpackChunkName: "about" */ '../views/skill/index.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/skillLearn/:id/:sectionId',
    name: 'skillLearn',
    component: () => import(/* webpackChunkName: "about" */ '../views/skill/learn_detail_page.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/skillPage/:id',
    name: 'skillPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/skill/skill_page.vue'),
    children: [
      {
        path: 'summary',
        name: 'skillPage-summary',
        component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_summary.vue'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: 'learn',
        name: 'skillPage-learn',
        component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_learn.vue'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: 'note',
        component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_note.vue'),
        children: [
          {
            path: 'list',
            name: 'skillPage-note-list',
            component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_note_list.vue'),
            meta: {
              requireAuth: true
            },
          },
          {
            path: 'detail/:noteId',
            name: 'skillPage-note-detail',
            component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_note_detail.vue'),
            meta: {
              requireAuth: true
            },
          },
        ]
      },
      {
        path: 'questions',
        component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_questions.vue'),
        children: [
          {
            path: 'list',
            name: 'skillPage-questions-list',
            component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_questions_search.vue'),
            meta: {
              requireAuth: true
            },
          },
          {
            path: 'editPage/:questionId',
            name: 'skillPage-questions-edit',
            component: () => import(/* webpackChunkName: "about" */ '../components/skill/skill_page/detail/skill_page_questions_edit.vue'),
            meta: {
              requireAuth: true
            },
          }
        ]
      }
    ]
  },
  {
    path: '/',
    redirect: '/userLogin',
    name: 'UserLogin'
  },
  {
    path: '/userLogin',
    name: 'UserLogin',
    component: () => import(/* webpackChunkName: "about" */ '../views/user/skill/login.vue')
  },
  {
    path: '/userRegister',
    name: 'UserRegister',
    component: () => import(/* webpackChunkName: "about" */ '../views/user/skill/register.vue')
  },
  {
    path: '/adminLogin',
    name: 'adminLogin',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/login.vue')
  },{
    path: '/admin/skill/guide',
    name: 'adminGuide',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/guide.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/admin/skill/skillManager',
    name: 'skillManager',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/skill/skill_manager.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/admin/skill/skillEdit/:id',
    name: 'skillEdit',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/skill/skill_edit.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/admin/skill/skillAdd',
    name: 'skillAdd',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/skill/skill_edit.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/admin/skill/questionManager',
    name: 'questionManager',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/skill/question_manager.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/admin/skill/questionEdit/:id',
    name: 'skillEdit',
    component: () => import(/* webpackChunkName: "about" */ '../views/admin/skill/question_edit.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/notfound',
    name: 'NotFound',
    component: () => import('../views/error/page404')
  },
  {
    path: '/500error',
    name: '500Error',
    component: () => import('../views/error/page500')
  },
  {
    path: '*',
    redirect: '/notfound',
    component: () => import('../views/error/page404')
  }
]

const router = new VueRouter({
  mode:'history',
  routes
})

// 钩子
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
      const last_time = JSON.parse(window.localStorage.getItem("time"));
      if (new Date().getTime()-last_time>3000*60*40){
        if(to.path.startsWith('/admin')){
          next({
            path: '/adminLogin',
            query: {redirect: to.fullPath}
          })
        } else {
          next({
            path: '/userLogin',
            query: {redirect: to.fullPath}
          })
        }
      } else {
        if (to.path.startsWith('/admin')) {
          if (store.state.admin.adminname) {
            next()
          } else {
            next({
              path: '/adminLogin',
              query: {redirect: to.fullPath}
            })
          }
        } else {
          if (store.state.user.username) {
            next()
          } else {
            next({
              path: '/userLogin',
              query: {redirect: to.fullPath}
            })
          }
        }
      }
    } else {
      next()
    }
  }
)

export default router
